﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Pur;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageAPRequest : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinBillingPaidApplicationEntity mSelectedBillingApplication;
        #endregion

        #region Constructor
        public frmManageAPRequest()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PAYING", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPAY_TYPE, "PAT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "PAYING", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND BPA.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (string.IsNullOrEmpty(txtInvoiceNo.Text) == false)
            {
                condition += " AND BPA.INVOICE_NO LIKE '%" + txtInvoiceNo.Text + "%'";
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtApplicationPerson.Tag;
                condition += " AND BPA.APPLICATION_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND BPA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND BPA.STATUS != 'PAYING05'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND BPA.APPLICATION_DATE >= CONVERT(DATETIME,'" + dtpApplicationDate.EditValue.ToString() + "')";
            }
            if (dtpApplicationDateTo.EditValue != null)
            {
                condition += " AND BPA.APPLICATION_DATE <= CONVERT(DATETIME,'" + dtpApplicationDateTo.EditValue.ToString() + "')";
            }
            if (string.IsNullOrEmpty(txtApplicationCode.Text) == false)
            {
                condition += " AND BPA.APPLICATION_CODE LIKE '%" + txtApplicationCode.Text + "%'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "BPA.APPLICATION_CODE DESC";
            DataTable dt = FinBillingPaidApplicationBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnPay.Enabled = false;
            btnInvalid.Enabled = false;
            if (mSelectedBillingApplication == null)
            {
                return;
            }
            if (mSelectedBillingApplication.Status == "PAYING01"
                || mSelectedBillingApplication.Status == "PAYING03")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedBillingApplication.Status == "PAYING02")
            {
                btnPay.Enabled = true;
                btnInvalid.Enabled = true;
            }
            else if (mSelectedBillingApplication.Status == "PAYING04")
            {
                btnInvalid.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = (DataRow)gridView1.GetDataRow(e.RowHandle);
            mSelectedBillingApplication = new FinBillingPaidApplicationEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPRequest frm = new frmMaintainAPRequest();
            frm.ThisActionType = ActionType.View;
            frm.SelectedBillingApplication = mSelectedBillingApplication;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中付款申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedBillingApplication.Status = "PAYING05";
                mSelectedBillingApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedBillingApplication.LastModifiedDate = DateTime.Now;
                bool successful = FinBillingPaidApplicationBs.UpdateFinBillingPaidApplication(mSelectedBillingApplication);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPRequest frm = new frmMaintainAPRequest();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedBillingApplication = mSelectedBillingApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPRequest frm = new frmMaintainAPRequest();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedBillingApplication = mSelectedBillingApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainAPRequest frm = new frmMaintainAPRequest();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAPRequest frm = new frmMaintainAPRequest(true);
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedBillingApplication = mSelectedBillingApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnInvalid_Click(object sender, EventArgs e)
        {
            if (mSelectedBillingApplication == null)
            {
                MessageBox.Show("请选择一个付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedBillingApplication.Status != "PAYING04")
            {
                MessageBox.Show("请选择一个已审已付的付款申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定作废？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                string oldStatus = mSelectedBillingApplication.Status;
                mSelectedBillingApplication.Status = "PAYING99";
                mSelectedBillingApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedBillingApplication.LastModifiedDate = DateTime.Now;
                bool successful = FinBillingPaidApplicationBs.InvalidBillingApplication(mSelectedBillingApplication, oldStatus);
                if (successful)
                {
                    MessageBox.Show("作废成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

    }
}
